<?php
/**
 * @author Tibor Pino (tibor.pino@gmail.com)
 * @license released under GNU Public License v. 2
 * @copyright Copyright (c) 2007, Tibor Pino
 *
 */

 uses('view/helpers/time');
 
 class DateHelper extends TimeHelper
 {

 	function niceShort($date_string = null, $format = null) {
 		if($format==null) $format=Configure::read('Date.short');
 		$date = $date_string ? $this->fromString($date_string) : time();
 		$ret = date($format, $date);
 		
 		return $this->output($ret);
 	}
 	
 	function nice($date_string = null, $format = null)	{
 		if($format==null) $format=Configure::read('Date.long');
 		$date = $date_string ? $this->fromString($date_string) : time();
 		$y = $this->isThisYear($date) ? '' : ' Y';

 		if ($this->isToday($date)) {
 			$ret = __('TODAY',true)." " . date("H:i", $date);
 		} elseif ($this->wasYesterday($date)) {
 			$ret = __('YESTERDAY',true)." " . date("H:i", $date);
 		} else {
 			$ret = date($format, $date);
 		}

 		return $this->output($ret);
 	}
 	
 	function timeAgoInWords($datetime_string, $format = null, $backwards = false) {
 		if($format==null) $format=Configure::read('Date.long');
		$datetime = $this->fromString($datetime_string);
		$in_seconds = $datetime;

		if ($backwards) {
			$diff = $in_seconds - time();
		} else {
			$diff = time() - $in_seconds;
		}

		$months = floor($diff / 2419200);
		$diff -= $months * 2419200;
		$weeks = floor($diff / 604800);
		$diff -= $weeks * 604800;
		$days = floor($diff / 86400);
		$diff -= $days * 86400;
		$hours = floor($diff / 3600);
		$diff -= $hours * 3600;
		$minutes = floor($diff / 60);
		$diff -= $minutes * 60;
		$seconds = $diff;

		if ($months > 0) {
			// over a month old, just show date (mm/dd/yyyy format)
			$relative_date = date($format, $in_seconds);
			$old = true;
		} else {
			$relative_date = '';
			$old = false;

			if ($weeks > 0) {
				// weeks and days
				$relative_date .= ($relative_date ? ', ' : '') . $weeks . ' ' . ($weeks > 1 ? __('DATE_WEEKS',true) : __('DATE_WEEK',true));
				$relative_date .= $days > 0 ? ($relative_date ? ' '.__('DATE_SEPARATOR',true).' ' : '') . $days . ' ' . ($days > 1 ? __('DATE_DAYS',true) : __('DATE_DAY',true)) : '';
			} elseif ($days > 0) {
				// days and hours
				$relative_date .= ($relative_date ? ', ' : '') . $days . ' ' . ($days > 1 ? __('DATE_DAYS',true) : __('DATE_DAY',true));
				$relative_date .= $hours > 0 ? ($relative_date ? ' '.__('DATE_SEPARATOR',true).' ' : '') . $hours . ' ' . ($hours > 1 ? __('DATE_HOURS',true) : __('DATE_HOUR',true)) : '';
			} elseif ($hours > 0) {
				// hours and minutes
				$relative_date .= ($relative_date ? ', ' : '') . $hours . ' ' . ($hours > 1 ? __('DATE_HOURS',true) : __('DATE_HOUR',true));
				$relative_date .= $minutes > 0 ? ($relative_date ? ' '.__('DATE_SEPARATOR',true).' ' : '') . $minutes . ' ' . ($minutes > 1 ? __('DATE_MINUTES',true) : __('DATE_MINUTE',true)) : '';
			} elseif ($minutes > 0) {
				// minutes only
				$relative_date .= ($relative_date ? ', ' : '') . $minutes . ' ' . ($minutes > 1 ? __('DATE_MINUTES',true) : __('DATE_MINUTE',true));
			} else {
				// seconds only
				$relative_date .= ($relative_date ? ', ' : '') . $seconds . ' ' . ($seconds != 1 ? __('DATE_SECOND',true) : __('DATE_SECONDS',true));
			}
		}

		$ret = $relative_date;
		// show relative date and add proper verbiage
		if (!$backwards && !$old) {
			$ret = __('DATE_AGO',true) . ' ' . $ret;
		}
		return $this->output($ret);
	}
 }

 ?>